package com.ficus.road.maintain.service.carmanager;

import com.ficus.road.maintain.core.model.carmanager.CarStatus;
import com.ficus.road.maintain.dao.carmanager.CarStatusDao;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author xiawen yang
 * @date 2022/2/17 下午4:59
 */
@Service
public class CarStatusService {
    private static String UNBIND_CAR_ID = "";
    private static String UNBIND_USER_ID = "";
    @Autowired
    private CarStatusDao carStatusDao;
    /**
     * 12h
     */
    @Value("${car_manager.bind_timeout}")
    private String bindTimeout;

    public CarStatus findBindCarStatusByCarId(String carId, long validTimestamp) {
        Query query = new Query(Criteria.where("car_id").is(carId).and("timestamp").gte(validTimestamp));
        query.with(new Sort(Sort.Direction.DESC, "timestamp")).skip(0).limit(1);
        CarStatus one = carStatusDao.findOne(query);
        return one;
    }

    public CarStatus findBindCarStatusByUserId(String userId, long validTimestamp) {
        Query query = new Query(Criteria.where("userid").is(userId).and("timestamp").gte(validTimestamp));
        query.with(new Sort(Sort.Direction.DESC, "timestamp")).skip(0).limit(1);
        CarStatus one = carStatusDao.findOne(query);
        return one;
    }

    public void createCarStatus(String userId, String carId, long timestamp) {
        CarStatus carStatus = CarStatus.builder().carId(carId).userid(userId).timestamp(timestamp).build();
        carStatusDao.saveOrUpdate(carStatus);
    }

    public CarStatus queryBindUserByCarId(String carId) {
        long validTimestamp = System.currentTimeMillis() * 1000000 - TimeUnit.SECONDS.toNanos(Long.parseLong(bindTimeout));
        CarStatus carStatus = findBindCarStatusByCarId(carId, validTimestamp);
        if (ObjectUtils.isNotEmpty(carStatus) && StringUtils.isNotEmpty(carStatus.getUserid())) {
            CarStatus userStatus = findBindCarStatusByUserId(carStatus.getUserid(), validTimestamp);
            if (ObjectUtils.isNotEmpty(userStatus) && userStatus.getCarId().equals(carId)) {
                return userStatus;
            }
        }
        return null;
    }

    public CarStatus queryBindCarByUserId(String userId) {
        long validTimestamp = System.currentTimeMillis() * 1000000 - TimeUnit.SECONDS.toNanos(Long.parseLong(bindTimeout));
        CarStatus userStatus = findBindCarStatusByUserId(userId, validTimestamp);
        if (ObjectUtils.isNotEmpty(userStatus) && StringUtils.isNotEmpty(userStatus.getCarId())) {
            CarStatus carStatus = findBindCarStatusByCarId(userStatus.getCarId(), validTimestamp);
            if (ObjectUtils.isNotEmpty(carStatus) && userId.equals(carStatus.getUserid())) {
                return carStatus;
            }
        }
        return null;
    }
}
